package cn.rocky.prepare;

public class countDigits {

    public int[] countBits(int n) {
        int[] bits = new int[n + 1];
        for (int i = 0; i <= n; i++) {
            bits[i] = countOnes(i);
        }
        return bits;
    }

    public int countOnes(int x) {
        int ones = 0;
        while (x > 0) {
            x &= (x - 1);
            ones++;
        }
        return ones;
    }

    public static void main(String[] args) {
        countDigits countDigits = new countDigits();
        System.out.println(countDigits.countOnes(20));
        System.out.println(2 & 1); //10 & 01
    }
}
